// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using SoapTrace.Core.Communication;
using System;
using System.Net.Sockets;
using System.Threading;

namespace SoapTrace.Tests.Utils
{
	/// <summary>
	/// A derived class of <see cref="ProxyServer"/>
	/// </summary>
	/// <remarks><para>This class is used to test the functionality that
	/// is available on deriving from the <see cref="ProxyServer"/> 
	/// class.</para>
	/// <para>The three methods being tested are
	/// <see cref="TcpSocketConnection.BytesReceived"/>,
	/// <see cref="TcpSocketConnection.BytesSent"/> and
	/// <see cref="ProxyServer.CreateClientConnection"/></para>
	/// </remarks>
	public class DerivedProxyServer : ProxyServer
	{
		/// <summary>
		/// A client message arrived event.
		/// </summary>
		private AutoResetEvent m_ClientMessageArrivedEvent = new AutoResetEvent( false );

		/// <summary>
		/// A server message arrived event.
		/// </summary>
		private AutoResetEvent m_ServerMessageArrivedEvent = new AutoResetEvent( false );

		/// <summary>
		/// The client message received.
		/// </summary>
		private MockRemoteServerMessage m_ClientMessageReceived;

		/// <summary>
		/// The server message received.
		/// </summary>
		private MockRemoteServerMessage m_ServerMessageReceived;

		/// <summary>
		/// Creates a new instance of the <see cref="DerivedProxyServer"/>
		/// class.
		/// </summary>
		public DerivedProxyServer( )
		{
		}

		/// <summary>
		/// Waits for a client message to arrive.
		/// </summary>
		/// <param name="message">The client message received whilst
		/// waiting.</param>
		/// <param name="timeout">A timeout in milliseconds.</param>
		/// <returns>A status flag indicating whether a message arrived
		/// before timing out.</returns>
		public bool WaitForClientMessage( out string message, int timeout )
		{			
			bool Success = m_ClientMessageArrivedEvent.WaitOne( timeout, true );
			
			if( Success )
			{
				message = m_ClientMessageReceived.GetString( );
			}
			else
			{
				message = String.Empty;
			}
			
			return Success;	
		}

		/// <summary>
		/// Waits for a server message to arrive.
		/// </summary>
		/// <param name="message">The message received whilst
		/// waiting.</param>
		/// <param name="timeout">A timeout in milliseconds.</param>
		/// <returns>A status flag indicating whether a message arrived
		/// before timing out.</returns>
		public bool WaitForServerMessage( out string message, int timeout )
		{			
			bool Success = m_ServerMessageArrivedEvent.WaitOne( timeout, true );
			
			if( Success )
			{
				message = m_ServerMessageReceived.GetString( );
			}
			else
			{
				message = String.Empty;
			}
			
			return Success;	
		}

		/// <summary>
		/// Raises the client message received event.  Called by the
		/// <see cref="DerivedTcpSocketConnection"/> class on receiving a message.
		/// </summary>
		/// <param name="message">The message received.</param>
		public void OnClientMessageReceived( MockRemoteServerMessage message )
		{
			m_ClientMessageReceived = message;
			m_ClientMessageArrivedEvent.Set( );
		}

		/// <summary>
		/// Raises the server message received event.  Called by the
		/// <see cref="DerivedTcpSocketConnection"/> class on receiving a message.
		/// </summary>
		/// <param name="message">The message received.</param>
		public void OnServerMessageReceived( MockRemoteServerMessage message )
		{
			m_ServerMessageReceived = message;
			m_ServerMessageArrivedEvent.Set( );
		}

		/// <summary>
		/// Creates a <see cref="DerivedTcpSocketConnection"/> object when accepting a new
		/// client connection.
		/// </summary>
		/// <param name="socket">The accepted socket connection.</param>
		/// <returns>A new <see cref="DerivedTcpSocketConnection"/>.</returns>
		protected override TcpSocketConnection CreateClientConnection( Socket socket )
		{
			return new DerivedTcpSocketConnection( socket, this );
		}
	}
}
